package com.base.mine.fag;

import android.text.TextUtils;
import android.view.View;

import com.base.commonlib.base.BaseApp;
import com.base.commonlib.base.CBaseMvpFragment;
import com.base.commonlib.bean.User;
import com.base.commonlib.http.RealNetWorkUtil;
import com.base.commonlib.http.utils.CallBackImpl;
import com.base.commonlib.utils.RxBus2;
import com.base.commonlib.utils.TimePickUtil;
import com.base.mine.R;
import com.base.mine.databinding.FagModifybirthdayBinding;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;

/**
 * Created by weikailiang on 2020/3/26.
 */

public class ModifyBirthdayFragment extends CBaseMvpFragment{
    private FagModifybirthdayBinding mBinding;
    protected CompositeDisposable mCompositeDisposable=new CompositeDisposable();
    @Override
    public void init() {
        mBinding = (FagModifybirthdayBinding) mRootBinding;
    }

    @Override
    public void start() {
        if (!TextUtils.isEmpty(BaseApp.getUser().getBirthday())){
            mBinding.birthday.setText(BaseApp.getUser().getBirthday());
        }
        mBinding.birthday.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                TimePickUtil.pickDate(mContext,mBinding.birthday);
            }
        });
        mBinding.save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                modifyBirthday();
            }
        });
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.fag_modifybirthday;
    }

    private void modifyBirthday(){
        showPb_mes("正在保存数据");
        Map<String,Object> param = new HashMap<>();
        param.put("birthday",mBinding.birthday.getText().toString());
        Disposable enqueue = RealNetWorkUtil.get().method("api-auth/user/updateBirthday").params(param)
                .build().enqueue(String.class, new CallBackImpl<String>() {
                    @Override
                    public void onError(int code, String msg, String errMsg) {
                        hidePb_mes();
                        showMes(msg);
                    }

                    @Override
                    public void onNext(String o) {
                        hidePb_mes();
                        User user = BaseApp.getUser();
                        user.setBirthday(mBinding.birthday.getText().toString());
                        BaseApp.saveUser(user);
                        RxBus2.getInstance().post(BaseApp.getUser());
                        showMes("保存成功");
                        _mActivity.finish();
                    }
                });
        mCompositeDisposable.add(enqueue);
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        mCompositeDisposable.dispose();
    }

}
